import { modifyPassword } from '@/services/apis/api';
import { history, useModel } from '@umijs/max';
import { Button, Form, Input, message } from 'antd';
import md5 from 'md5';
export default () => {
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  let userid = initialState?.currentUser?.userid || '';
  const loginOut = async () => {
    localStorage.removeItem('token');
    history.replace({
      pathname: '/system/login',
    });
  };
  return (
    <div style={{ background: 'white' }}>
      <Form
        name="basic"
        labelWrap={true}
        labelCol={{ span: 2, offset: 0 }}
        style={{
          marginTop: '10px',
          backgroundColor: '#fff',
          padding: '10px 26px',
        }}
        onFinish={async (values) => {
          if (values.newPwd2 === values.newPwd) {
            let query: any = {
              id: userid,
              newPwd: md5(values.newPwd),
              oldPwd: md5(values.oldPwd),
            };
            try {
              let res = await modifyPassword(query);
              if (res.status === 0) {
                message.success('修改成功！');
                loginOut();
              } else {
                message.warning(res.message || '修改失败！');
              }
            } catch (error) {}
          } else {
            message.warning('两次密码不一致！');
          }
        }}
        labelAlign="right"
      >
        <Form.Item
          label="原密码"
          name="oldPwd"
          rules={[{ required: true, message: '请输入原密码!' }]}
        >
          <Input.Password style={{ width: '400px' }} />
        </Form.Item>
        <Form.Item
          label="新密码"
          name="newPwd"
          rules={[{ required: true, message: '请输入新密码!' }]}
        >
          <Input.Password style={{ width: '400px' }} />
        </Form.Item>
        <Form.Item
          label="再次输入新密码"
          name="newPwd2"
          rules={[{ required: true, message: '请输入新密码!' }]}
        >
          <Input.Password style={{ width: '400px' }} />
        </Form.Item>
        <Form.Item>
          <Button style={{ marginLeft: '280px' }} type="primary" htmlType="submit">
            保存
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};
